import * as spanService from '../../services/design/spanService';
import * as structureServices from '../../services/design/structureServices';
import { notification } from 'antd';
import _ from "lodash"
/***
 * 
 * station=>x;elevation=>y1;ground_elevation=>y2
 */
export default {

    namespace: 'refSpans',
    state: {
        spans: [],
        currentSpan: null,
        buildingSpans:[]
    },
    reducers: {
        updateProjectSpans(state, { payload: spans }) {
            return { ...state, spans: spans ? spans : [] };
        },
        setCurrent(state, { payload: span }) {
            return { ...state, currentSpan: span };
        },
        setBuildingSpans(state, { payload: spans }) {
            return { ...state, buildingSpans: spans };
        }
    },
    effects: {
        *getBridgeSpans({ payload: params }, { call, put, select }) {//获取桥梁构造物初始信息
            const res = yield call(spanService.getBridgeRoadSpans, { ...params });
            const offset = yield select(state => state.refRoads.offset)
            let spans = yield select(state => state.refSpans.spans)
            let bridge = spans.find(item => item.bridgeId == params.bridgeId)
            if (res && res.data && res.data.code == '200') {
                if (res.data.data && res.data.data.length) {
                    let data = res.data.data
                    if(offset){
                        for (let i = 0; i < data.length; i++) {
                            let item = data[i]
                            item.lines = [[item.station - offset.PK, item.elevation - offset.Z], [item.station - offset.PK, item.groundHeight - offset.Z]]
                            item.bridgeId = params.bridgeId
                        }
                        bridge.children = data
                    }                 
                    yield put({
                        type: 'updateProjectSpans',
                        payload: spans
                    });
                    yield put({
                        type: 'setBuildingSpans',
                        payload: data
                    });
                }
            }
        },
        *getTopSelections({ payload: values }, { call, put, select }) {
            const res = yield call(spanService.getTopSelections, { ...values });
            if (res && res.data && res.data.code == '200') {
                return res.data.data||[]
            }else{
                return []
            }
        },
        *getSelections({ payload: values }, { call, put, select }) {
            const res = yield call(spanService.getSelections, { ...values });
            if (res && res.data && res.data.code == '200') {
                return res.data.data||[]
            }else{
                return []
            }
        },
        *getBridgeSpansById({ payload: values }, { call, put, select }) {
            const res = yield call(spanService.getBridgeSpansById, { ...values });
            let spans = yield select(state => state.refSpans.spans)
            if (res && res.data && res.data.code == '200') {
                if (res.data.data && res.data.data.length) {
                    for (let i = 0; i < res.data.data.length; i++) {
                        let item = res.data.data[i];
                        item.bridgeId = values.bridgeId;
                    }
                    
                }
            }
            return res.data.data || []
        },
        *deployComponent({ payload: { spans, bridgeId } }, { call, put, select }) {
            if (spans && spans.length) {
                let nspans = _.cloneDeep(yield select(state => state.refSpans.buildingSpans))
                if (nspans && nspans.length) {
                    for (let i = 0; i < nspans.length; i++) {
                        let pier = nspans[i];
                        // console.log(spans)
                        // debugger
                        for (let j = 0; j < spans.length; j++) {
                            // debugger
                            let item2 = spans[j];
                            // debugger

                            if (pier.id == item2.id) {
                                nspans[i] = JSON.parse(JSON.stringify(item2))
                                // debugger
                            }
                        }
                    }
                }
                yield put({
                    type: 'updatebuildingSpans',
                    payload: nspans
                })
                const res = yield call(structureServices.deployComponent, nspans);
                if (res && res.data && res.data.data && res.data.data.length) {
                    yield put.resolve({
                        type: 'updatebuildingSpans',
                        payload: res.data.data
                    })
                }
                yield new Promise((resolve, reject) => {
                    resolve()
                });
                // debugger
            }

        },
        /**
         * 布跨配置
         * @param {*} param0 
         * @param {*} param1 
         */
        *configSpans({ payload: { values, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(spanService.configBridgeRoadSpans, values);
            if (res && res.data && res.data.code == '200') {
                onSuccess(res.data.data)

            } else {
                onError(res && res.data ? res.data.message : '服务器连接失败');
            }
        },
        *updateSpans({ payload: spans }, { call, put }) {
            yield put({
                type: 'updateProjectSpans',
                payload: spans
            });
        },
        *updateCurrent({ payload: span }, { call, put }) {
            yield put({
                type: 'setCurrent',
                payload: span
            });
        },
        *updatebuildingSpans({ payload: spans }, { call, put }) {
            yield put({
                type: 'setBuildingSpans',
                payload: spans
            });
        }
    }
}